﻿using System;
using System.Text;
using System.IO;
using System.Net;

namespace Music.Utility.Helper
{

    /// <summary>
    /// 版权所有: Copyright © 2018 Avenger. 保留所有权利。
    /// 内容摘要: 短信发送封装类
    /// 完成日期：2018年11月10日
    /// 版    本：V1.0.2 
    /// 作    者：Avenger
    /// </summary>
    public class SMSHelper
    {
        /// <summary>
        /// sms.cn注册账号
        /// </summary>
        public static string strSMSUID = "cloudin";
        /// <summary>
        /// sms.cn注册账号密码
        /// </summary>
        public static string strSMSPassword = "53344521";

        /// <summary>
        /// 注册发送验证码
        /// </summary>
        /// <param name="strMobile">手机号码</param>
        /// <param name="strCode">验证码</param>
        /// <returns></returns>
        public static string SendSMSForCode(string strMobile, string strCode)
        {
            string strReturnValue = "";

            try
            {
                string sendurl = "http://api.sms.cn/mt/";
                string strContent = "您的验证码是" + strCode + "。如非本人操作，请忽略本短信。【欧亚经济论坛】";
                StringBuilder sbTemp = new StringBuilder();
                string strPassword = MD5Helper.MD5Encrypt32(strSMSPassword + strSMSUID);
                sbTemp.Append("ac=send&uid=" + strSMSUID + "&pwd=" + strPassword + "&mobile=" + strMobile + "&content=" + strContent);
                byte[] bTemp = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sbTemp.ToString());
                strReturnValue = doPostRequest(sendurl, bTemp);
            }
            catch (Exception ex)
            {
                strReturnValue = ex.Message;
            }

            return strReturnValue;

        }

        /// <summary>
        /// 发送通用短信模板
        /// </summary>
        /// <param name="strMobile"></param>
        /// <param name="strContent"></param>
        /// <returns></returns>
        public static string SendSMSForContent(string strMobile, string strContent)
        {
            string strReturnValue = "";

            try
            {
                string sendurl = "http://api.sms.cn/mt/";
                StringBuilder sbTemp = new StringBuilder();
                string strPassword = MD5Helper.MD5Encrypt32(strSMSPassword + strSMSUID);
                sbTemp.Append("uid=" + strSMSUID + "&pwd=" + strPassword + "&mobile=" + strMobile + "&content=" + strContent);
                byte[] bTemp = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sbTemp.ToString());
                strReturnValue = doPostRequest(sendurl, bTemp);
            }
            catch (Exception ex)
            {
                strReturnValue = ex.Message;
            }

            return strReturnValue;

        }

        /// <summary>
        /// 嘉宾报名成功,等待审核
        /// </summary>
        /// <param name="strMobile"></param>
        /// <returns></returns>
        public static string SendSMSForWaitReview(string strMobile)
        {
            string strReturnValue = "";

            try
            {
                string sendurl = "http://api.sms.cn/mt/";
                string strContent = "您的报名资料已提交成功，等待审核。【欧亚经济论坛】";
                StringBuilder sbTemp = new StringBuilder();
                string strPassword = MD5Helper.MD5Encrypt32(strSMSPassword + strSMSUID);
                sbTemp.Append("ac=send&uid=" + strSMSUID + "&pwd=" + strPassword + "&mobile=" + strMobile + "&content=" + strContent);
                byte[] bTemp = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sbTemp.ToString());
                strReturnValue = doPostRequest(sendurl, bTemp);
            }
            catch (Exception ex)
            {
                strReturnValue = ex.Message;
            }

            return strReturnValue;

        }

        /// <summary>
        /// 嘉宾报名成功,报名审核通过
        /// </summary>
        /// <param name="strMobile"></param>
        /// <returns></returns>
        public static string SendSMSForReviewPass(string strMobile)
        {
            string strReturnValue = "";

            try
            {
                string sendurl = "http://api.sms.cn/mt/";
                string strContent = "恭喜您，您的报名资料已通过审核，请尽快上传您的核酸检测报告或接种新冠疫苗证明！【欧亚经济论坛】";
                StringBuilder sbTemp = new StringBuilder();
                string strPassword = MD5Helper.MD5Encrypt32(strSMSPassword + strSMSUID);
                sbTemp.Append("ac=send&uid=" + strSMSUID + "&pwd=" + strPassword + "&mobile=" + strMobile + "&content=" + strContent);
                byte[] bTemp = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sbTemp.ToString());
                strReturnValue = doPostRequest(sendurl, bTemp);
            }
            catch (Exception ex)
            {
                strReturnValue = ex.Message;
            }

            return strReturnValue;

        }

        /// <summary>
        /// 嘉宾报名成功,报名审核不通过
        /// </summary>
        /// <param name="strMobile"></param>
        /// <returns></returns>
        public static string SendSMSForReviewNotPass(string strMobile)
        {
            string strReturnValue = "";

            try
            {
                string sendurl = "http://api.sms.cn/mt/";
                string strContent = "对不起，您的报名资料未通过审核，请重新提交。【欧亚经济论坛】";
                StringBuilder sbTemp = new StringBuilder();
                string strPassword = MD5Helper.MD5Encrypt32(strSMSPassword + strSMSUID);
                sbTemp.Append("ac=send&uid=" + strSMSUID + "&pwd=" + strPassword + "&mobile=" + strMobile + "&content=" + strContent);
                byte[] bTemp = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sbTemp.ToString());
                strReturnValue = doPostRequest(sendurl, bTemp);
            }
            catch (Exception ex)
            {
                strReturnValue = ex.Message;
            }

            return strReturnValue;

        }

        /// <summary>
        /// 嘉宾报名成功,审核通过，政审未通过
        /// </summary>
        /// <param name="strMobile"></param>
        /// <returns></returns>
        public static string SendSMSForPoliticalExaminationNotPass(string strMobile)
        {
            string strReturnValue = "";

            try
            {
                string sendurl = "http://api.sms.cn/mt/";
                string strContent = "对不起，您的证审资料未通过审核。【欧亚经济论坛】";
                StringBuilder sbTemp = new StringBuilder();
                string strPassword = MD5Helper.MD5Encrypt32(strSMSPassword + strSMSUID);
                sbTemp.Append("ac=send&uid=" + strSMSUID + "&pwd=" + strPassword + "&mobile=" + strMobile + "&content=" + strContent);
                byte[] bTemp = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sbTemp.ToString());
                strReturnValue = doPostRequest(sendurl, bTemp);
            }
            catch (Exception ex)
            {
                strReturnValue = ex.Message;
            }

            return strReturnValue;

        }

        /// <summary>
        /// 嘉宾报名成功,审核通过，政审通过
        /// </summary>
        /// <param name="strMobile"></param>
        /// <param name="qrcodeUrl"></param>
        /// <returns></returns>
        public static string SendSMSForPoliticalExaminationPass(string strMobile, string qrcodeUrl)
        {
            string strReturnValue = "";

            try
            {
                string sendurl = "http://api.sms.cn/mt/";
                string strContent = "恭喜您，您的证审资料已审核通过。二维码：" + qrcodeUrl + "【欧亚经济论坛】";
                StringBuilder sbTemp = new StringBuilder();
                string strPassword = MD5Helper.MD5Encrypt32(strSMSPassword + strSMSUID);
                sbTemp.Append("ac=send&uid=" + strSMSUID + "&pwd=" + strPassword + "&mobile=" + strMobile + "&content=" + strContent);
                byte[] bTemp = System.Text.Encoding.GetEncoding("utf-8").GetBytes(sbTemp.ToString());
                strReturnValue = doPostRequest(sendurl, bTemp);
            }
            catch (Exception ex)
            {
                strReturnValue = ex.Message;
            }

            return strReturnValue;

        }

        /// <summary>
        /// Post方式请求，并返回结果
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="bData">提交数据</param>
        /// <returns>返回结果</returns>
        public static string doPostRequest(string url, byte[] bData)
        {
            HttpWebRequest hwRequest = null;
            HttpWebResponse hwResponse = null;
            string strResultString = "";

            hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            hwRequest.Timeout = 5000;
            hwRequest.Method = "POST";
            hwRequest.ContentType = "application/x-www-form-urlencoded";
            hwRequest.ContentLength = bData.Length;
            System.IO.Stream smWrite = hwRequest.GetRequestStream();
            smWrite.Write(bData, 0, bData.Length);
            smWrite.Close();

            hwResponse = (HttpWebResponse)hwRequest.GetResponse();
            StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
            strResultString = srReader.ReadToEnd();
            srReader.Close();
            hwResponse.Close();

            return strResultString;
        }
    }
}
